package com.tarena.lbs.content.service.impl;

import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.dao.repository.CategoryRepository;
import com.tarena.lbs.content.pojo.po.ArticleCategoryPO;
import com.tarena.lbs.content.pojo.query.ArticleCategoryQuery;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import com.tarena.lbs.content.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    private CategoryRepository categoryRepository;

    @Override
    public PageResult<ArticleCategoryVO> pageList(ArticleCategoryQuery articleCategoryQuery) {
        PageInfo<ArticleCategoryPO> pageInfo = categoryRepository.pageList(articleCategoryQuery);
        PageResult<ArticleCategoryVO> pageResult = new PageResult<>();
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setPageNo(pageInfo.getPageNum());
        pageResult.setPageSize(pageInfo.getPageSize());
        List<ArticleCategoryVO> vos = null;
        if ((!CollectionUtils.isEmpty(pageInfo.getList()))){
            vos = pageInfo.getList().stream().map(po -> {
                ArticleCategoryVO vo = new ArticleCategoryVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        pageResult.setObjects(vos);

        return pageResult;
    }
}
